#ifndef _USB_SERIAL_H
#define _USB_SERIAL_H

#include <stdint.h>

enum
{
	STATE_RECV_HEAD,
	STATE_RECV_LEN,
	STATE_RECV_CMD,
	STATE_RECV_DATA,
	STATE_RECV_CHECKSUM,
};

typedef struct
{
	uint8_t status;
	uint8_t length;
	uint8_t cmd;
	uint8_t idx;
	uint8_t data[64];
	uint8_t checksum;
} SerialData_t;

typedef struct
{
	int16_t PosA;
	int16_t PosB;
	int16_t Speed;
	uint32_t Tick;
	uint32_t Delay;
	uint32_t RemainDelay;
	uint32_t flag;
	uint32_t pi_up;
	uint32_t pi_dn;
    uint32_t backlash_flag;
    int16_t backlash_pi_high_pos;
    int16_t backlash_pi_low_pos;
} MotorTask_t;

extern void usb_serial_rx_handler(uint8_t *buf,uint16_t len);

extern void usb_serial_update_af_ring(void);
extern void usb_serial_update_af_ring_err(void);

extern void usb_serial_update_tick_af(void);
extern void usb_serial_update_tick_ap(void);

extern void Motor_task_set_idle_af(void);
extern void Motor_task_set_idle_ap(void);

extern void Motor_task_pi_calc_af(uint32_t level);
extern void Motor_task_pi_calc_ap(uint32_t level);
extern void af_ring_detection(int pi_3_level,int pi_4_level);

extern void backlash_ap_handler(void);
extern void backlash_af_handler(void);
extern void backlash_ap_pos_update(void);
extern void backlash_af_pos_update(void);

extern void usb_serial_update_af_reverse_error(int16_t reverse_error);
extern void usb_serial_update_adc_ring_ap(int16_t adc_value);
extern void usb_serial_update_adc_ring_af(int16_t adc_value);
extern void usb_serial_update_btn_af_mf(int16_t btn_level);
extern void usb_serial_update_btn_Fn(int16_t btn_level);
extern void usb_serial_update_ap_pi_and_pos(void);
extern void usb_serial_update_af_pi_and_pos(void);
extern void usb_serial_update_ring_adc_snake_ap(uint16_t snake);
extern void usb_serial_update_ring_adc_snake_af(uint16_t snake);

extern void Motor_task_callback(void);
extern void af_ring_init(void);

extern void serial_recv_handler(SerialData_t *pSerialRecvData);

#endif
